#include "../include/head.h"
// 链表相关
// 创建链表函数
LinkList createList()
{
    LinkList head = (LinkList)malloc(sizeof(LNode));
    memset(&head->data, 0, sizeof(LNode));
    head->next = NULL;
    head->prior = NULL;
    return head;
}
int list_length(LinkList H)
{
    int i = 0;
    LinkList p = H->next;
    while (p)
    {
        p = p->next;
        i++;
    }
    return i;
}
// 入库
int entry_goods(LinkList H, goodsNode value)
{

    LinkList head = H->next;
    int flag = 0;
    while (head)
    {
        if (strcmp(head->data.name, value.name) == 0)
        {
            flag = 1;
            break;
        }
        head = head->next;
    }

    if (flag)
    {
        printf("找到商品，原数量：%d，将为您增加数量，并且价格和位置以新入库为准,价格：%f,位置：%s\n", head->data.num, head->data.price, head->data.location);
        head->data.num = head->data.num + value.num;
        head->data.price = value.price;
        strcpy(head->data.location, value.location);
    }
    else
    {
        // printf("库内未找到该商品，插入数据\n");
        list_head_insert(H, value);
    }
    write_data(L);
    printf("入库成功！\n");
}
// 头插
int list_head_insert(LinkList H, goodsNode value)
{
    LinkList newNode = (LinkList)malloc(sizeof(LNode));
    LinkList head = H;
    if (head == NULL)
    {
        printf("空间有问题\n");
    }

    if (newNode == NULL)
    {
        printf("内存分配失败\n");
        return -1;
    }
    // 头插数据

    // // 设置新节点数据
    newNode->data = value;
    newNode->prior = NULL;
    newNode->next = NULL;
    // // 设置新节点的next指向头结点的下一个结点
    newNode->next = head->next;
    if (head->next != NULL)
    {
        head->next->prior = newNode;
    }
    // // 新节点的前驱指向头结点
    newNode->prior = head;
    // // 头结点的后驱指向新节点
    head->next = newNode;

    return 0;
}
// 尾插
int list_tail_insert(LinkList H, goodsNode value)
{
    LinkList head = H;
    LinkList node = (LinkList)malloc(sizeof(LNode));
    if (node == NULL)
    {
        printf("内存分配失败\n");
        return -1;
    }
    node->data = value;
    node->next = NULL;

    while (head->next)
    {
        head = head->next;
    }
    node->prior = head;
    head->next = node;
}
// 根据名称查找
LinkList list_find_by_name(LinkList L, char *name)
{
    int flag = 0;
    LinkList head = L;
    while (head->next)
    {
        if (strcmp(head->next->data.name, name) == 0)
        {
            flag = 1;
            return head->next;
        }
        head = head->next;
    }
    if (flag)
    {
        return head;
    }
    else
    {
        return NULL;
    }
}
// 减少商品数量
int list_reduce_num(LinkList L, char *name, int num)
{
    LinkList head = L;
    while (head->next)
    {
        if (strcmp(head->next->data.name, name) == 0)
        {
            if (head->next->data.num >= num)
            {

                head->next->data.num -= num;
                printf("出库成功！\n");
                return 0;
            }
            else
            {
                printf("库存不足！\n");
                return -1;
            }
        }
        head = head->next;
    }
    return -1;
}
// 删除商品
int list_delte_by_name(LinkList L, char *name)
{
    LinkList head = L;
    char type = 'N';
    int flag = 0;
    while (head->next)
    {
        if (strcmp(head->next->data.name, name) == 0)
        {
            flag = 1;
            break;
        }
        head = head->next;
    }
    LinkList t = head->next; // 用t存储待删除点
    if (flag)
    {
        printf("是否确定删除%s(Y/N)？\n", name);
        scanf(" %c", &type);
        if (type == 'Y')
        {
            if (head->next->next)
            {
                printf("不是最后一个节点\n");
                head->next = head->next->next;
                head->next->prior = head;
                free(t);
                t = NULL;
                return 1;
            }
            else
            {
                head->next->prior = head;
                head->next = NULL;
                free(t);
                t = NULL;
                return 1;
            }
        }
        return 0;
    }
    else
    {
        printf("未找到需要删除的文件!\n");
        return 0;
    }
}
// 修改商品
int list_change_by_name(LinkList L, goodsNode value)
{
    LinkList head = L->next;
    char type = 'N';
    while (head)
    {
        if (strcmp(head->data.name, value.name) == 0)
        {
            break;
        }
        head = head->next;
    }
    printf("是否确定修改%s(Y/N)？\n", value.name);
    scanf(" %c", &type);
    if (type == 'Y')
    {
        strcpy(head->data.name, value.name);
        head->data.price = value.price;
        strcpy(head->data.location, value.location);
        head->data.num = value.num;
        return 1;
    }
    return -1;
}
void list_printf_change_page(LinkList L, int pageNo, int pageSize, int showPageNums, LinkList p)
{
    // 打印上边框
    for (size_t i = 0; i < 70; i++)
    {
        printf("=");
    }
    printf("\n");
    printf("名称\t\t\t价格\t\t货架位置\t\t数量\n"); // 简化版，可能需要根据实际终端调整制表符宽度

    for (size_t i = 0; i < 70; i++)
    {
        printf("-");
    }
    printf("\n");
    printf("/*/-----%s,%d,%d\n", p->data.name, showPageNums, (pageNo * pageSize));
    while (p && showPageNums < (pageNo * pageSize))
    {
        printf("%-23s", p->data.name);
        printf("%-18.2f", p->data.price);
        printf("%-23s", p->data.location);
        printf("%-16d", p->data.num);
        printf("\n");
        p = p->next;
        showPageNums++;
    }
    for (size_t i = 0; i < 70; i++)
    {
        printf("-");
    }
    printf("\n");
    printf("上一页before\t页码:%d,展示的条数:%d,每页条数:%d,链表长度:%d,%s\t下一页after\n", pageNo, showPageNums, pageSize, list_length(L), p->data.name);
    // 打印下边框
    for (size_t i = 0; i < 70; i++)
    {
        printf("=");
    }
    printf("\n");
    char action[8];
    scanf("%s", action);
    if (strcmp(action, "before") == 0)
    {
        if (pageNo > 1)
        {
            for (size_t i = 0; i < pageSize; i++)
            {
                // 往回移位置
                
                p = p->prior;
                showPageNums--;
                showPageNums--;
            }
            pageNo--;
            printf("pageNo:%d\n", pageNo);
            list_printf_change_page(L, pageNo, pageSize, showPageNums, p->prior);
        }
    }
    else if (strcmp(action, "after") == 0)
    {
        if (showPageNums < list_length(L))
        {
            printf("*-*\n");
            pageNo++;
            list_printf_change_page(L, pageNo, pageSize, showPageNums, p->prior);
        }
    }
}
void list_printf(LinkList L)
{
    int pageNo = 1;       // 第几页
    int pageSize = 2;     // 每页条数
    int showPageNums = 0; // 当前展示条数
    // 打印上边框
    for (size_t i = 0; i < 70; i++)
    {
        printf("=");
    }
    printf("\n");
    printf("名称\t\t\t价格\t\t货架位置\t\t数量\n"); // 简化版，可能需要根据实际终端调整制表符宽度

    for (size_t i = 0; i < 70; i++)
    {
        printf("-");
    }
    printf("\n");

    LinkList p = L->next;

    while (p && showPageNums < (pageNo * pageSize))
    {
        printf("%-23s", p->data.name);
        printf("%-18.2f", p->data.price);
        printf("%-23s", p->data.location);
        printf("%-16d", p->data.num);
        printf("\n");
        p = p->next;
        showPageNums++;
    }
    for (size_t i = 0; i < 70; i++)
    {
        printf("-");
    }
    printf("\n");
    printf("上一页before\t页码:%d,展示的条数:%d,每页条数:%d,链表长度:%d,%s\t下一页after\n", pageNo, showPageNums, pageSize, list_length(L), p->data.name);
    // 打印下边框
    for (size_t i = 0; i < 70; i++)
    {
        printf("=");
    }
    printf("\n");
    char action[8];
    scanf("%s", action);
    if (strcmp(action, "before") == 0)
    {
        if (pageNo > 1)
        {
            for (size_t i = 0; i < pageSize; i++)
            {
                // 往回移位置
                p = p->prior;
                showPageNums--;
                showPageNums--;
            }
            pageNo--;
            printf("pageNo:%d\n", pageNo);

            list_printf_change_page(L, pageNo, pageSize, showPageNums, p);
        }
    }
    else if (strcmp(action, "after") == 0)
    {

        if (showPageNums < list_length(L))
        {
            pageNo++;
            list_printf_change_page(L, pageNo, pageSize, showPageNums, p);
        }
    }
}
